package _0000_study._leetcode._swordoffer._022_getKthFromEnd;

/**
 * Date: 2021/9/2 9:23
 * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
 * 内存消耗：36.4 MB, 在所有 Java 提交中击败了19.75%的用户
 */
public class Solution {

    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode q = head;
        for (int i = 0; i < k; i++) q = q.next;
        ListNode p = head;
        while (q != null) {
            p = p.next;
            q = q.next;
        }
        return p;
    }

    public static void main(String[] args) {

        ListNode head = (new MyLinkedList(new int[]{1, 2, 3, 4, 5})).getHead();
        MyLinkedList.printLinkedList((new Solution()).getKthFromEnd(head, 2));
    }
}
